package com.xiejr.actuator.socket.config;

import cn.hutool.core.lang.Assert;
import com.xiejr.actuator.annotations.EnableTopicSocket;
import com.xiejr.actuator.annotations.TopicHandler;
import com.xiejr.actuator.socket.Topic;
import com.xiejr.actuator.socket.scanners.SocketClasspathScanner;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.AssignableTypeFilter;

/**
 * @program: socket-actuator-spring-boot-starter
 * @description: 主题式socket注册
 * @author: xjr
 * @create: 2020-07-13 15:32
 **/

public class TopicSocketRegister implements ImportBeanDefinitionRegistrar {

    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        AnnotationAttributes annotationMap= AnnotationAttributes.fromMap(annotationMetadata.getAnnotationAttributes(EnableTopicSocket.class.getName()));
        String packageName=annotationMap.getString("value");
        Assert.notEmpty("packageName must be not null");
        SocketClasspathScanner socketClasspathScanner=new SocketClasspathScanner(beanDefinitionRegistry);
        socketClasspathScanner.registerFilter(new AssignableTypeFilter(Topic.class));
        socketClasspathScanner.registerFilter(new AnnotationTypeFilter(TopicHandler.class));
        socketClasspathScanner.scan(packageName);
    }
}
